import wx
import widget

import resource.string.app as r
import resource.icon as icon
import resource.toolbar as toolbar

import __miyuki as view

class Miyuki(widget.Frame):
    """Miyuki view
    
    Attributes:
        models: Collection of post model instance.
        model: Post model instance to be displayed.
        
    Methods:
        delete: Clear image and info and change post list icon to cross-script.
    """
    
    def __init__(self):
        super(Miyuki, self).__init__(None, name='Miyuki')
        self.Title = '%s Version %s' % (r.name, r.version.human)
        self.Icon = icon.Main.Icon
        self._realize()
    
    def _realize(self):
        self.MenuBar = view.MenuBar()
        self.ToolBar = view.ToolBar(self)
        self.ToolBar.Realize()
        
        self.StatusBar = view.StatusBar(self)
        
        style = wx.SP_LIVE_UPDATE
        self.splitter = wx.SplitterWindow(self, style=style)
        self.splitter.SetMinimumPaneSize(50)
        
        self.leftSplitter = wx.SplitterWindow(self.splitter, style=style)
        self.leftSplitter.SetMinimumPaneSize(50)
        
        self.postsPanel = wx.Panel(self.leftSplitter)
        
        self.posts = view.Posts(self.postsPanel)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.posts, 1, wx.GROW)
        
        self.postsPanel.SetSizer(sizer)
        
        self.infoPanel = wx.Panel(self.leftSplitter)
        
        self.info = view.Info(self.infoPanel)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.info, 1, wx.GROW)
        
        self.infoPanel.SetSizer(sizer)
        
        self.leftSplitter.SplitHorizontally(self.postsPanel, self.infoPanel)
        
        self.image = view.Image(self.splitter)
        
        self.splitter.SplitVertically(self.leftSplitter, self.image)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.splitter, 1, wx.GROW)
        
        self.SetSizer(sizer)
        
    def orientSplit(self, mode=None):
        """Orient splitter
        
        Args:
            mode: An optional constant of wx indicating vertical or horizontal.
                wx.SPLIT_VERTICAL or wx.SPLIT_HORIZONTAL
        """
        
        if not self.splitter.IsSplit(): return
        
        ver = wx.SPLIT_VERTICAL
        hor = wx.SPLIT_HORIZONTAL
        if not mode:
            current = self.splitter.SplitMode
            mode = ver if current == hor else hor
        
        rHor = toolbar.ApplicationSplit.Bitmap
        rVer = toolbar.ApplicationSplitVertical.Bitmap
        split = self.MenuBar.file.orientSplit
        split.Bitmap = rVer if mode == ver else rHor
        
        self.leftSplitter.SetSplitMode(ver if mode == hor else hor)
        
        self.splitter.SetSplitMode(mode)
        self.splitter.Freeze()
        self.Layout()
        self.splitter.Thaw()
    
    def showSplitter(self):
        """Show splitter"""
        
        splitter = self.splitter
        if not splitter.IsSplit():
            splitter.Freeze()
            self.leftSplitter.Show()
            if splitter.SplitMode == wx.SPLIT_HORIZONTAL:
                splitter.SplitHorizontally(self.leftSplitter, self.image)
            else:
                splitter.SplitVertically(self.leftSplitter, self.image)
            splitter.Thaw()
        self.MenuBar.file.toggleSplit.Bitmap = toolbar.ArrowOut.Bitmap
        
    def hideSplitter(self):
        """Hide splitter"""
        
        splitter = self.splitter
        if splitter.IsSplit():
            splitter.Freeze()
            splitter.Unsplit(self.leftSplitter)
            splitter.Thaw()
        self.MenuBar.file.toggleSplit.Bitmap = toolbar.ArrowIn.Bitmap
       
    _models = []
    
    @property
    def models(self):
        return self._models
    
    @models.setter
    def models(self, models):
        self._models = models
        self.posts.models = models
        
    _model = None
    
    @property
    def model(self):
        return self._model
    
    @model.setter
    def model(self, model):
        self._model = model
        self.info.model = model
        self.image.model = model
